﻿namespace com.stifel.ServerBuildMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.stifel.GlobalDao;

    using Microsoft.SharePoint.Linq;

    using Context = StifelServerBuildDataContext;

    using CustomObject = StifelVirtualServerLocation;

    public class StifelVirtualServerLocationDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelvirtualserverlocation";

        #endregion Fields

        #region Constructors

        public StifelVirtualServerLocationDao( )
            : base(ListName)
        {
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelVirtualServerLocationDao( string currentWebUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public void Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                l.InsertOnSubmit( value );
                c.SubmitChanges( );
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<CustomObject> l = c.GetList<CustomObject>( ListTitle ).ToList( );
                TimezoneDao tzDao = new TimezoneDao( CurrentWebUrl );
                USStatesDao usDao = new USStatesDao( CurrentWebUrl );
                l.ForEach( a =>
                {
                    if ( a.TimeZoneLookupId.HasValue )
                        a.Timezone = tzDao.Get( a.TimeZoneLookupId.Value );
                    if ( a.USStatesLookupId.HasValue )
                        a.USStateAbbreviation = usDao.Get( a.USStatesLookupId.Value ).USStateAbbreviation;
                } );
                return l;
            }
        }

        public CustomObject GetByTitle( string title )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                  where o.Title.Equals( title, StringComparison.OrdinalIgnoreCase )
                                                  select o ).SingleOrDefault( );
                return v;
            }
        }

        #endregion Methods
    }
}